#include "Sample.h"

const DWORD dwHotClearColor = 0xff0000ff;
const DWORD dwColdClearColor = 0xff000080;

Sample::Sample(void)
{
	m_bLeftActive		= TRUE;
	m_Camera.InIt();
}

Sample::~Sample(void)
{

}

bool Sample::Init()
{
	m_Terrain.Init();
	m_pFrustum.SetDevice( this->m_pd3dDevice );	
	m_Camera.SetDevice( this->m_pd3dDevice );
	

	if( !m_Terrain.Create( this->m_pd3dDevice,  _T("Map512.bmp"), _T("Map512.jpg"), m_Camera.m_pFrustum))
	{
		return false;
	}

	m_Camera.SetDevice( this->m_pd3dDevice );
	m_Camera.InIt();
	m_Camera.CreateViewMatrix( D3DXVECTOR3( 0.0f, 100.0f, -10.0f ), D3DXVECTOR3( 0.0f, 0.0f, 0.0f ) );
	m_Camera.CreateProjecTionMatrix();

	m_MinimapCamera.SetDevice( this->m_pd3dDevice );
	m_MinimapCamera.InIt();
	m_MinimapCamera.CreateViewMatrix( D3DXVECTOR3( 0.0f, 200.0f, -70.0f ), D3DXVECTOR3( 0.0f, 0.0f, 0.0f ) );
	m_MinimapCamera.CreateProjecTionMatrix();

	m_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
	m_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
	m_Camera.Frame();

	m_pd3dDevice->GetViewport( &m_BackBufferVP );
	return true;
}

bool Sample::Frame()
{
	m_Terrain.Frame();
	return true;  
}

bool Sample::Render()
{	
	RenderScene();
	m_Terrain.BoundingBoxRender( m_Terrain.m_pQuadTree );

	// Draw MiniMap viewport 
	D3DVIEWPORT9		vp;
	vp.X = 0;
	vp.Y = 0;
	vp.Width = 300;
	vp.Height = 300;
	vp.MinZ = 0.0f;
	vp.MaxZ = 1.0f;

	m_pd3dDevice->SetViewport( &vp );
	m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, m_bLeftActive ? dwHotClearColor : dwColdClearColor, 1.0f, 0L );
	m_pd3dDevice->SetTransform( D3DTS_VIEW, &m_MinimapCamera.matView );
	m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &m_MinimapCamera.matProj );

	RenderScene();
	m_Camera.Render();

	m_pd3dDevice->SetViewport( &m_BackBufferVP );
	m_pd3dDevice->SetTransform( D3DTS_VIEW, &m_Camera.matView );
	m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &m_Camera.matProj );

	return true;  
}
void Sample::RenderScene()
{
	m_Terrain.Render();
}


bool Sample::Release()
{
	m_MinimapCamera.Release();
	m_Terrain.Release();
	return true;  
}

INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
	Sample gSample;
	if( !gSample.CreateWin(hInst,_T("SampleQuadTree")))
	{
		return 0;
	}
	if( !gSample.CreateDevice())
	{
		return 0;
	}
	gSample.Run();
	return 1;
}